<?php

class Application_Model_BolaoJogoMapper
{
    protected $_dbTable;

    public function setDBTable($tabelaBD)
    {
        if(is_string($tabelaBD)){
            $tabelaBD = new $tabelaBD();
        }
        if (!$tabelaBD instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Tabela invalida');
        }
        $this->_dbTable = $tabelaBD;
        return $this;
    }

    public function getDBTable()
    {
        if (null == $this->_dbTable) {
            $this->setDBTable('Application_Model_DbTable_BolaoJogo');
        }
        return $this->_dbTable;
    }
    
    public function getBoloesPendentes($email, $total, $inicio)
    {
        $tabela = $this->getDBTable();

        $select = $tabela->select();
        $select->from($tabela, '*')
               ->where('email_criador is not null')
               ->where('email_participante = ?', $email)
               ->where('data > NOW()')
               ->order('data ASC')
               ->limit($total, $inicio);

        return $tabela->fetchAll($select);
    }

    public function getNumeroBoloesPendentes()
    {
        $tabela = $this->getDBTable();

        $select = $tabela->select();
        $select->from($tabela, '*')
               ->where('email_participante = ?', $email)
               ->where('data > NOW()')
               ->where('email_criador is not null');

        $linha =  $tabela->fetchAll($select);

        return count($linha);
    }

    public function getBoloesAdmImportante($total, $inicio)
    {
        $tabela = $this->getDBTable();

        $select = $tabela->select();
        $select->from($tabela, '*')
               ->where('email_criador is null')
               ->where('importante = ?', true)
               ->where('data > NOW()')
               ->order('data ASC')
               ->limit($total, $inicio);

        return $tabela->fetchAll($select);
    }

    public function getNumeroBoloesAdmImportante()
    {
        $tabela = $this->getDBTable();

        $select = $tabela->select();
        $select->from($tabela, '*')
               ->where('data > NOW()')
               ->where('importante = ?', true)
               ->where('email_criador is null');

        $linha =  $tabela->fetchAll($select);

        return count($linha);
    }

    public function getBoloesAdmMaisApostados($total, $inicio)
    {
        $tabela = $this->getDBTable();

        
        $select = $tabela->select();
        $select->from($tabela, '*')
               ->where('email_criador is null')
               ->where('data > NOW()')               
               ->order('n_apostas DESC')
               ->order('data ASC')
               ->limit($total, $inicio);

        return $tabela->fetchAll($select);
    }

    public function getBoloesAdm($total, $inicio)
    {
        $tabela = $this->getDBTable();

        $select = $tabela->select();
        $select->from($tabela, '*')
               ->where('email_criador is null')
               ->where('data > NOW()')
               ->order('data ASC')
               ->limit($total, $inicio);

        return $tabela->fetchAll($select);
    }

    public function getNumeroBoloesAdmMaisApostados()
    {
        $tabela = $this->getDBTable();

        $select = $tabela->select();
        $select->from($tabela, '*')
               ->where('data > NOW()')
               ->where('email_criador is null');

        $linha =  $tabela->fetchAll($select);

        return count($linha);
    }

    public function getBoloesCampeonato($campeonato, $total, $inicio)
    {
        $tabela = $this->getDBTable();

        $select = $tabela->select();
        $select->from($tabela, '*')
               ->where('email_criador is null')
               ->where('data > NOW()')
               ->where('nome_campeonato = ?', $campeonato)
               ->order('data ASC')
               ->limit($total, $inicio);

        return $tabela->fetchAll($select);
    }


    public function getNumeroBoloesCampeonato($campeonato)
    {
        $tabela = $this->getDBTable();

        $select = $tabela->select();
        $select->from($tabela, '*')
               ->where('email_criador is null')
               ->where('data > NOW()')
               ->where('nome_campeonato = ?', $campeonato)
               ->order('data ASC');

        $linha =  $tabela->fetchAll($select);

        return count($linha);
    }

    public function findBolao($id, $id_jogo, $somenteFuturos = false)
    {
        $tabela = $this->getDBTable();

        $select = $tabela->select();
        $select->from($tabela, '*')
               ->where('id = ?', $id)
               ->where('id_jogo = ?', $id_jogo);

        if ($somenteFuturos) {
            $select->where('data > NOW()');
        }


        $linha = $tabela->fetchAll($select);

        if(count($linha) == 0)
            return;
        
        return $linha->current();
    }

}

